package com.qyc.Microservice.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.AbstractEnvironment;

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;


/**
 * 访问地址：
 *   /swagger-ui/index.html
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {
	
    @Autowired
    private AbstractEnvironment env;
    
    @Bean
    public Docket createRestApi() {
    	//是否开启swagger，正式环境一般是需要关闭的（避免不必要的漏洞暴露！），可根据springboot的多环境配置进行设置
    	String swaggerflag = env.getProperty("swagger.enable");
    	
    	if("true".equals(swaggerflag)){
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.qyc.Microservice.controller"))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title("项目接口接口说明")
                            .description("详细信息......")
                            .version("1.0")
                            .contact(new Contact("JoePotter","https://www.cnblogs.com/JoePotter/","252177507@qq.com"))
                            .license("The Apache License")
                            .licenseUrl("https://www.cnblogs.com/JoePotter/")
                            .build());
    	}else {
            return new Docket(DocumentationType.SWAGGER_2)
                    .pathMapping("/")
                    .select()
                    .apis(RequestHandlerSelectors.basePackage("com.qyc.Microservice.controller"))
                    .paths(PathSelectors.any())
                    .build().apiInfo(new ApiInfoBuilder()
                            .title("项目接口接口说明")
                            .description("详细信息......")
                            .version("1.0")
                            .contact(new Contact("JoePotter","https://www.cnblogs.com/JoePotter/","252177507@qq.com"))
                            .license("The Apache License")
                            .licenseUrl("https://www.cnblogs.com/JoePotter/")
                            .build()).enable(false);
    	}
    }
}
